/**
 * WapOptionsScreen.java
 * The options screen for the httpdemo.
 * 
 * Copyright ?1998-2008 Research In Motion Ltd.
 * 
 * Note: For the sake of simplicity, this sample application may not leverage
 * resource bundles and resource strings.  However, it is STRONGLY recommended
 * that application developers make use of the localization features available
 * within the BlackBerry development platform to ensure a seamless application
 * experience across a variety of languages and geographies.  For more information
 * on localizing your application, please refer to the BlackBerry Java Development
 * Environment Development Guide associated with this release.
 */

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import javax.microedition.io.*;
import java.io.*;
import java.util.*;
import javax.microedition.rms.*;

/**
 * A simple options screen that allows the user to specify the WAP parameters to
 * use when opening an HTTP connection.
 */

public final class OptionsScreen extends MainScreen {
	// Cconstants
	// ----------------------------------------------------------------

	// Members
	// ------------------------------------------------------------------
	private UiApplication _app;
	private EditField _username;
	private PasswordEditField _password;
	private EditField _api;

	private MainScreen _this;
	private MenuItem _save;
	private RecordStore record;

	private boolean isNew;

	// Constructors
	// -------------------------------------------------------------
	public OptionsScreen(UiApplication uiapp) {
		super();
		// try{
		// RecordStore.deleteRecordStore("user_pass");}catch (Exception e){}
		_this = this;
		_app = uiapp;

		// Instantiate some cached menu items.
		_save = new MenuItem("Save", 105, 10) {
			public void run() {
				save();
				_app.popScreen(_this);
			}
		};

		setTitle(new LabelField("Options", LabelField.ELLIPSIS
				| LabelField.USE_ALL_WIDTH));

		_username = new EditField("Username: ", "");
		_password = new PasswordEditField("Password: ", "");
		_api = new EditField("Api: ", "");
		add(_username);
		add(_password);
		add(_api);

		// load persistant data for username and password
		try {

			record = RecordStore.openRecordStore("user_pass", true);
			// RecordStore.deleteRecordStore("user_pass");
			// return;
			System.out.println(record.getNumRecords());
		} catch (Exception e) {
		}

		try {
			if (record.getNumRecords() == 0) {
				isNew = true;
			} else {
				isNew = false;
			}
		} catch (Exception e) {
		}
		if (isNew) {
			_api
					.setText("http://sms.api.bz/fetion.php?username=%user%&password=%pass%&sendto=%num%&message=%message%");
		} else {
			try {
				byte user[] = record.getRecord(1);
				_username.setText(new String(user));
				byte pass[] = record.getRecord(2);
				_password.setText(new String(pass));
				byte api[] = record.getRecord(3);
				_api.setText(new String(api));
			} catch (Exception e) {
			}
		}
		addMenuItem(_save);
	}

	// Methods
	// ------------------------------------------------------------------
	public void display() {
		_app.pushScreen(this);
	}

	/**
	 * Returns a preformatted wap parameter string, appropriate to append to an
	 * HTTP Connector.open string.
	 * 
	 * @return a preformatted wap parameter string.
	 */

	public String getUsername() {
		return _username.getText().trim();
	}

	public String getPassword() {
		return _password.getText().trim();
	}

	public String getApiTemplate() {
		return _api.getText().trim();
	}

	/**
	 * Called when there have been changes and users saves screen.
	 */
	public void save() {

		byte[] user = _username.getText().trim().getBytes();
		byte[] pass = _password.getText().trim().getBytes();
		byte[] api = _api.getText().trim().getBytes();

		try {
			if (isNew) {
				record.addRecord(user, 0, user.length);
				record.addRecord(pass, 0, pass.length);
				record.addRecord(api, 0, api.length);
			} else {
				record.setRecord(1, user, 0, user.length);
				record.setRecord(2, pass, 0, pass.length);
				record.setRecord(3, api, 0, api.length);
			}

		} catch (Exception e) {
			System.out.println(e.toString());
		}
		isNew = false;
	}
}
